package com.cskaoyan.controller.marketingmanagement;

import com.cskaoyan.bean.marketingmanagementbean.AdData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * 创建日期: 2022/05/05 20:15
 *
 * @author liutao
 */
@RestController
@RequestMapping("admin")
public class AdController {


    @Autowired
    AdService adService;


    @RequestMapping("ad/list")
    public BaseRespVo adList(BaseParam param, String name, String content) {
        AdData adData = adService.query(param, name, content);
        return BaseRespVo.ok(adData);
    }


    @RequestMapping("storage/create")
    public BaseRespVo createStorage(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        Integer size = ((int) file.getSize());
        String contentType = file.getContentType();
        // 图片存放位置
        file.transferTo(new File("/Users/liutao/Documents/git-test/one-piece/mall38/target/classes/image", originalFilename));
        // file.transferTo(new File("/Users/liutao/Documents/git-test/one-piece/mall38/src/main/resources/image", originalFilename));
        MarketStorage marketStorage = new MarketStorage(null, originalFilename, originalFilename, contentType, size, "http://localhost:8083/image/" + originalFilename, new Date(), new Date(), false);
        adService.createStorage(marketStorage);
        return BaseRespVo.ok(marketStorage);
    }

    @RequestMapping("ad/create")
    public BaseRespVo createAd(@RequestBody MarketAd marketAd) {

        // 将marketAd存储到数据库
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
        marketAd.setDeleted(false);
        adService.createAd(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    @RequestMapping("ad/update")
    public BaseRespVo adUpdate(@RequestBody MarketAd marketAd) {
        marketAd.setUpdateTime(new Date());
        adService.adUpdate(marketAd);
        return BaseRespVo.ok(marketAd);

    }

    @RequestMapping("ad/delete")
    public BaseRespVo delete(@RequestBody MarketAd marketAd) {
        adService.delete(marketAd);
        return BaseRespVo.ok(marketAd);
    }

}
